package io.github.wslxm.springbootplus2.manage.sys.controller;

import io.github.wslxm.springbootplus2.core.base.controller.BaseController;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.constant.BaseConstant;
import io.github.wslxm.springbootplus2.core.result.Result;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysUserDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysUserQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysUserVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户
 *
 * @author 王松
 * @WX-QQ 1720696548
 * @date 2019/11/13 13:38
 */
@RestController
@RequestMapping(BaseConstant.Uri.API_ADMIN + "/sys/user")
@Tag(name = "base--sys--用户管理")
public class SysUserController extends BaseController<SysUserService> {


    @GetMapping(value = "/findPage")
    @Operation(summary = "列表查询")
    public Result<BasePage<SysUserVO>> findPage(@ModelAttribute @Validated SysUserQuery query) {
        return Result.successFind(baseService.findPage(query));
    }


    @GetMapping(value = "/{id}")
    @Operation(summary = "ID查询")
    public Result<SysUserVO> findId(@PathVariable String id) {
        return Result.success(baseService.findId(id));
    }


    @PostMapping
    @Operation(summary = "添加")
    public Result<String> insert(@RequestBody @Validated SysUserDTO dto) {
        return Result.successInsert(baseService.insert(dto));
    }


    @PutMapping(value = "/{id}")
    @Operation(summary = "ID编辑")
    public Result<Boolean> upd(@PathVariable String id, @RequestBody SysUserDTO dto) {
        dto.setPassword(null);
        return Result.successUpdate(baseService.upd(id, dto));
    }


    @DeleteMapping(value = "/{id}")
    @Operation(summary = "ID删除")
    public Result<Boolean> del(@PathVariable String id) {
        return Result.successDelete(baseService.del(id));
    }


    @GetMapping(value = "/findByRoleId")
    @Operation(summary = "获取指定角色的用户列表")
    @Parameter(name = "roleId", description = "角色Id", required = false)
    @Deprecated
    public Result<List<SysUserVO>> findByRoleId(@RequestParam String roleId) {
        return Result.success(XjBeanUtil.listVo(baseService.findByRoleId(roleId), SysUserVO.class));
    }


    @GetMapping(value = "/list/keyData")
    @Operation(summary = "查询所有-只返回关键数据(姓名/昵称/电话/id)")
    @Parameters({
            @Parameter(name = "searchName", description = "姓名or用户名", required = false, example = ""),
    })
    public Result<List<SysUserVO>> listKeyData(@RequestParam(required = false) String searchName) {
        return Result.success(baseService.listKeyData(searchName));
    }


    @PutMapping(value = "/{id}/resetPassword")
    @Operation(summary = "重置任意用户密码")
    public Result<Boolean> updResetPassword(@PathVariable String id, @RequestParam String password) {
        return Result.success(baseService.updResetPassword(id, password));
    }

}
